//
//  SYP_WallpaperViewController.m
//  wind
//
//  Created by SYP on 16/1/15.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "SYP_WallpaperViewController.h"

@interface SYP_WallpaperViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) UITableView *tableV;
@end



@implementation SYP_WallpaperViewController

- (void)dealloc
{
    Block_release(myImageBlock);
    [_imageV release];
    [_tableV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"更换壁纸";
    [self.navigationController setToolbarHidden:YES animated:YES];
    [self creatTableView];
}

# pragma 创建tableView
- (void)creatTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.tableHeaderView.hidden = NO;
    
    self.tableV.tableHeaderView = [self creatHeadView];
        
    [self.view addSubview:self.tableV];
    [_tableV release];
}

#pragma 创建头视图
- (UIView *)creatHeadView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2)];
    view.backgroundColor = [UIColor redColor];
    
    self.imageV = [[UIImageView alloc] init];
    self.imageV.frame = view.frame;
    NSString *imagefile = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents" ] stringByAppendingPathComponent:@"myImage"];
    
    UIImage *image = [[[UIImage alloc] initWithContentsOfFile:imagefile] autorelease];
    self.imageV.image = image;
    [view addSubview:self.imageV];
    [_imageV release];
    
    
    
    return view;
}

// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 3;
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    if (0 == indexPath.row) {
        cell.textLabel.text = @"从相册选择";
    } else if (1 == indexPath.row){
        cell.textLabel.text = @"拍一张";
        
    } else {
        cell.textLabel.text = @"退出";
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


#pragma "拍一张"的实现方法
- (void)cameraAction
{
    // 判断是否有摄像头
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIImagePickerController *imagePic = [[UIImagePickerController alloc] init];
        imagePic.delegate = self;
        imagePic.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:imagePic animated:YES completion:^{
            
            
        }];
        
    } else {
        if (IOS8) {
            
        }
    }
}


#pragma 点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        NSLog(@"从相册选一张");
        UIImagePickerController *imagePicVC = [[UIImagePickerController alloc] init];
        imagePicVC.delegate = self;
        [self presentViewController:imagePicVC animated:YES completion:^{
            
            
        }];
    
        
    } else if (1 == indexPath.row){
        NSLog(@"拍一张");
        [self cameraAction];
        
    } else {
        
        self.navigationController.toolbarHidden = NO;
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

// 选取照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    
    UIAlertController *alertActionC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"用作壁纸" preferredStyle:UIAlertControllerStyleAlert];

    [alertActionC addAction:[UIAlertAction actionWithTitle:@"重新选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }]];
    [self presentViewController:alertActionC animated:YES completion:^{
        
        
    }];
    
    
    
    
    
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [self saveImage:image withName:@"myImage"];
    
    self.imageV.image = image;
    
    myImageBlock(image);

    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

#pragma 保存图片到沙盒
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 1);
    NSString *imagefile = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    [imageData writeToFile:imagefile atomically:NO];
}



#pragma 将选中的图片传给主页的头
- (void)sendImageBlock:(imageBlock)imageBlock
{
    myImageBlock = Block_copy(imageBlock);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
